package by.grsu.hdm.devices;

import java.util.Random;

import by.grsu.hdm.exception.IllegalInternalStateException;
import by.grsu.hdm.media.FileAudioVideo;
import by.grsu.hdm.model.HomeDevice;
import by.grsu.hdm.model.IMediaPlayerAbility;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.model.IRemoteControl;
import by.grsu.hdm.model.IUsbPort;
import by.grsu.hdm.model.IUsbSupport;
import by.grsu.hdm.model.UsbPort;

public class MediaCenter extends HomeDevice implements IUsbSupport, INetworkAvailable, IMediaPlayerAbility, IRemoteControl{
	
	private long trafficIn;
	private long trafficOut;
	private boolean access = false;
	
	private UsbPort usbPort;
	
	protected MediaCenter() {
		defaultConfiguration();
	}
	
	public MediaCenter(String id) {
		super(id);
		
		defaultConfiguration();
	}
	
	protected void defaultConfiguration()
	{
		this.trafficIn = 0;
		this.trafficOut = 0;
		
		this.usbPort = null;
	}
	
	@Override
	public long getOutgoingTraffic() {
		return this.trafficOut;
	}
	
	@Override
	public long getIncomingTraffic() {
		return this.trafficIn;
	}

	@Override
	protected void afterTurningOn() {
		this.trafficIn = Math.abs(new Random().nextInt());
		this.trafficOut = Math.abs(new Random().nextInt());
	}

	@Override
	protected void beforeTurningOff() {
		
		defaultConfiguration();
	}

	@Override
	public IUsbPort getUsbPort() {
		if (this.usbPort == null) { 
			this.usbPort = new UsbPort("USB-" + getDeviceId());
		}
		
		return this.usbPort;
	}

	@Override
	public String play(FileAudioVideo file){
		this.access=true;
		return (getDeviceId() + file.getMessage(file.getName(), file.getFormat()));
	}
	
	@Override
	public void buttonPlay() throws IllegalInternalStateException{
		if(access == false){
			throw new IllegalInternalStateException();
		}
		System.out.println("You press play. "+ getDeviceId() +" play.");	
	}

	@Override
	public void buttonPause() throws IllegalInternalStateException{
		if(access == false){
			throw new IllegalInternalStateException();
		}
		System.out.println("You press pause. "+ getDeviceId() +" suspended.");
	}

	@Override
	public void buttonStop() throws IllegalInternalStateException{
		if(access == false){
			throw new IllegalInternalStateException();
		}
		System.out.println("You press stop. "+ getDeviceId() +" stopped playing.");
	}


}
